package com.zcm.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zcm.entity.address;
import com.zcm.service.UserService;

/**
 * Servlet implementation class DoAddAddress
 */
@WebServlet("/DoAddAddress")
public class DoAddAddress extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoAddAddress() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int userid = Integer.parseInt(request.getParameter("userid"));
		String  name = request.getParameter("name");
		String  province = request.getParameter("province");
		String  phonenum = request.getParameter("phonenum");
		String  zone = request.getParameter("zone");
		String  zone2 = request.getParameter("zone2");
		String  stree = request.getParameter("stree");
		String  zipcode = request.getParameter("zipcode");
		String  alabel = request.getParameter("alabel");
		address a = new address();
		a.setAlabel(alabel);
		a.setName(name);
		a.setPhonenum(phonenum);
		a.setProvince(province);
		a.setStree(stree);
		a.setUserid(userid);
		a.setZipcode(zipcode);
		a.setZone(zone);
		a.setZone2(zone2);
		UserService us = new UserService();
		if(us.AddAddress(a)>0) {
			request.setAttribute("msg", "地址添加成功");
			request.setAttribute("userid", userid);
		}else {
			request.setAttribute("msg", "地址添加失败");
			request.setAttribute("userid", userid);
		}
		List list = new ArrayList<>();
		list = us.SelectAddressByUserID(userid);
		request.setAttribute("list", list);
		request.setAttribute("userid",userid);
		request.getRequestDispatcher("/Pay.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
